// 1、无头单向非循环链表实现
public class SingleLinkedList {
    /**
     * 节点内部类
     */
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    //设置个头
    ListNode head ;

    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }

    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        ListNode cur = head;
        if(this.head ==  null){
            head.next = node;
        }else {
            while(cur.next != null){
                cur = cur.next;
            }
                cur.next = node;
        }
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        ListNode cur = head;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        cur.val =data;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode cur = head;
        //零个结点
        if(head == null){
//            head = null;
            return;
        }
        //一个结点
        if( head.next == null){
            if (head.val == key){
                head = null;
            }
            return;
        }
        //两个及以上
        ListNode nextNode = head.next;
        //首个就是key
        if(cur.val == key){
            head = nextNode;
        }
        while(cur != null){
            if(nextNode.val == key){
                cur.next = nextNode.next;
                return;
            }
            cur = cur.next;
            nextNode = cur.next;
        }
    }

    //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode cur = head;
        //零个结点
        if(head == null){
//            head = null;
            return;
        }
        //一个结点
        if( head.next == null){
            if (head.val == key){
                head = null;
            }
            return;
        }

        //两个及以上
        ListNode nextNode = head.next;
        while(nextNode != null){
            if(nextNode.val == key){
                cur.next = nextNode.next;
                nextNode = nextNode.next;
            }
            else{
                cur = nextNode;
                nextNode = nextNode.next;
            }
            if(this.head.val == key) {
                this.head = this.head.next;
            }
        }


    }
    //得到单链表的长度
    public int size(){
        ListNode cur = head;
        int size = 0;
        while (cur != null){
            size++;
            cur = cur.next;
        }
        return size;
    }


    public void display(){
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void clear(){
        this.head = null;
    }
}